package utilities;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;


public class ConvertService {
	public static String stringFromStream (InputStream is) throws IOException
	{
		//BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));

	    StringBuilder sb = new StringBuilder();
	    String line = null;
	    while ((line = reader.readLine()) != null) {
	      sb.append(line + "\n");
	    }
	    is.close();
	    return sb.toString();
	}
	
	public static String toUTF8(String isoString)
	  {
	   String utf8String = null;
	   if (null != isoString && !isoString.equals(""))
	   {
	    try
	    {
	     byte[] stringBytesISO = isoString.getBytes("ISO-8859-1");
	     utf8String = new String(stringBytesISO, "UTF-8");
	    }
	    catch(UnsupportedEncodingException e)
	    {
	     //  TODO: This should never happen. The UnsupportedEncodingException
	     // should be propagated instead of swallowed. This error would indicate
	     // a severe misconfiguration of the JVM.

	     // As we can't translate just send back the best guess.
	     System.out.println("UnsupportedEncodingException is: " +
	 e.getMessage());
	     utf8String = isoString;
	    }
	   }
	   else
	   {
	    utf8String = isoString;
	   }
	   return utf8String;
	  } 
}
